﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class Department_DepartmentSet : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["action"] != null)
            {
                BindDept(Request["action"].ToString());
                DptForm.Visible = true;
            }
            else
            {

                DptForm.Visible = false;
            }
            DepartmentTreeView.Bind(DepartmentTreeView.OnlyDpt);

        

            MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
            master.HelpHref = "";
            master.PageName = "部门设置";
            master.PicUrl = "~/image/PageIcon/dep.jpg";
            master.IntiBar();
        }
       

    }


    private void BindDept(string Did)
    {

        object[] obj = new object[] { Did };


        object[] ret = Srvtools.CliUtils.CallMethod("SDepartment", "GetDepartmentDetail", obj);

        DataTable dt = ((DataSet)ret[1]).Tables[0];

        if (dt.Rows.Count <= 0)
        {
            return;
        }

        DptForm.Visible = true;

        Cancel.Enabled = false;

        Add.Enabled = true;

        DepartmentId.Text = dt.Rows[0]["DepartmentId"].ToString();

        SelUser.SelUserInti(dt.Rows[0]["Manager"].ToString(), dt.Rows[0]["EmpName"].ToString());

        DepartmentName.Text = dt.Rows[0]["DepartmentName"].ToString();

        DepartmentDes.Text = dt.Rows[0]["DepartmentDes"].ToString();

        dpt.ddlSelectValue(dt.Rows[0]["ParentDepartId"].ToString());

        Del.SelectedValue = "0";

        Save.CommandArgument = dt.Rows[0]["DepartmentId"].ToString() + "," + dt.Rows[0]["ParentDepartId"].ToString() + "," + SaveMode.Save;

        Save.Attributes.Add("onclick", "return confirm('操作请注意：\\r\\r1、部门变更，员工的部门编号、职位编号也将会批量更改" +
            "\\r\\r2、部门变更时，如变更部门下存在子部门，变更失败\\r\\r3、部门作废时，如作废部门存在子部门及作废部门下有未离职员工，作废失败\\r\\r4、一旦作废，无法复原\\r\\r\\r确认执行？')");

        if (Did == "0")
        {
            dpt.ShowTextOnly("0");
        }

        dptMsg.Text = "批处理部门变更！";


        DelMsg.Text = "禁用将无法复原！";

        lblMode.Text = "部门变更";
    }

    private enum SaveMode : int
    {
        Save,
        AddChild

    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string[] SaveInfo = Save.CommandArgument.ToString().Split(',');

        if (SaveInfo.Length <= 0)
        {
            return;
        }


        if (SaveInfo[2] == SaveMode.Save.ToString())
        {
            ChangeDpt(SaveInfo[0], SaveInfo[1]);
        }
        else if (SaveInfo[2] == SaveMode.AddChild.ToString())
        {
            AddChildDpt(SaveInfo[1]);
        }


    }


    private void ChangeDpt(string CurrentDid, string OldDptId)
    {
        object[] ret = null;
        try
        {
            if (Del.SelectedValue == "1")
            {
                ret = Srvtools.CliUtils.CallMethod("SDepartment", "GetEmpExist", new object[] { CurrentDid });
                DataTable Dt = ((DataSet)ret[1]).Tables[0];

                if (Convert.ToInt32(Dt.Rows[0][0]) > 0)
                {
                    Del.SelectedValue = "0";
                    throw new Exception("作废失败！作废部门下不得存在未离职员工");
                }
                else if (Convert.ToInt32(Dt.Rows[1][0]) > 0)
                {
                    Del.SelectedValue = "0";
                    throw new Exception("作废失败！作废部门下不得存子部门");
                }
                else
                {
                    object[] obj = new object[] { CurrentDid, DepartmentName.Text.Trim(), DepartmentDes.Text.Trim(), Del.SelectedValue, "0", "0", OldDptId, "0", SelUser.EmployeeId };
                    ret = Srvtools.CliUtils.CallMethod("SDepartment", "UpdateDpt", obj);
                }

                if ((bool)ret[1])
                {
                    ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('部门作废成功');location.href='DepartmentSet.aspx'", true);
                }
                else
                {
                    Del.SelectedValue = "0";
                    throw new Exception();
                }
            }
            else
            {
                if (dpt.SelectDepmanetId != OldDptId)
                {
                    object[] obj = new object[] { CurrentDid, DepartmentName.Text.Trim(), DepartmentDes.Text.Trim(), "0", "0", "0", OldDptId, "0", dpt.SelectDepmanetId, SelUser.EmployeeId };
                    ret = Srvtools.CliUtils.CallMethod("SDepartment", "TransMoveDpt", obj);
                }
                else
                {
                    object[] obj = new object[] { CurrentDid, DepartmentName.Text.Trim(), DepartmentDes.Text.Trim(), Del.SelectedValue, "0", "0", OldDptId, "0", SelUser.EmployeeId };
                    ret = Srvtools.CliUtils.CallMethod("SDepartment", "UpdateDpt", obj);
                }
                if ((bool)ret[1])
                {
                    ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('部门变更成功');location.href='DepartmentSet.aspx'", true);
                }

            }
        }
        catch (Exception ex)
        {
            if (ex.Message.IndexOf("Error") != -1)
            {
                ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('" + ex.Message.Substring(ex.Message.IndexOf("Error")) + "')", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('" + ex.Message + "')", true);
            }
        }
    }

    private void AddChildDpt(string ParentDptId)
    {
        object[] obj = new object[] { "0", DepartmentName.Text.Trim(), DepartmentDes.Text.Trim(), "0", "0", "0", ParentDptId, "0", SelUser.EmployeeId };


        object[] ret = Srvtools.CliUtils.CallMethod("SDepartment", "AddChildDpt", obj);

        if ((bool)ret[1])
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('子部门添加成功');location.href='DepartmentSet.aspx'", true);
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('子部门添加失败');location.href='DepartmentSet.aspx'", true);
        }
    }

    protected void Add_Click(object sender, EventArgs e)
    {
        Add.Enabled = false;
        Del.Enabled = false;

        Cancel.Enabled = true;


        dpt.ShowTextOnly(Request["action"].ToString());

        Save.CommandArgument = "0," + Request["action"].ToString() + "," + SaveMode.AddChild;

        Save.Attributes.Add("onclick", "return confirm('确认执行吗？')");

        DepartmentName.Text = "";

        DepartmentDes.Text = "";

        Del.SelectedValue = "0";

        SelUser.EmpName = "";

        SelUser.EmployeeId = "";

        dptMsg.Text = "";

        TrDptId.Visible = false;

        DelMsg.Text = "";

        lblMode.Text = "子部门添加";

    }

    protected void Cancel_Click(object sender, EventArgs e)
    {
        TrDptId.Visible = true;
        Cancel.Enabled = false;
        Add.Enabled = true;
        Del.Enabled = true;
        //BindDept(dpt.SelectDepmanetIdHid);
    }

    protected void LinkButton1_Click1(object sender, EventArgs e)
    {
        SelUser.EmpName = "";

        SelUser.EmployeeId = "";

    }
}
